﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESRI.ArcGIS;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Server;
using ESRI.ArcGIS.Framework;
using ESRI.ArcGIS.CatalogUI;

using mapXBase;
using mc_basXcls7Lib;
using mc_basObj7Lib;
using System.Collections;
using mx_MapLibCtrlLib;
using System.Diagnostics;
using System.IO;
using System.Threading;


namespace ConvertProject
{
    public partial class form1 : Form
    {

        private IMapDocument m_MapDocument; //arggis对应的工程文件
        int GCScode;   //坐标系对应的值

        public form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            
        }
      

        private void progressBar1_Click(object sender, EventArgs e)
        {
        }

        //选择工程文件
        private void selectprj_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgOpenFile = new OpenFileDialog { Filter = "工程文件(*.mpj)|*.mpj" };
            dlgOpenFile.Title = "选择需要转换的工程文件";
            if (dlgOpenFile.ShowDialog() == DialogResult.OK)
            {
                this.mpjpath.Text = null;
                this.mpjpath.Text = dlgOpenFile.FileName;
            }
        }

        //选择保存路径
        private void savapath_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog path = new FolderBrowserDialog();
            if (path.ShowDialog() == DialogResult.OK)
            {
                this.savepath.Text = null;
                this.savepath.Text = path.SelectedPath;
            }
        }

        //选择style文件
        private void selectstyle_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlgOpenStyle = new OpenFileDialog { Filter = "style文件(.style)|*.style" };
            dlgOpenStyle.Title = "选择工程对应的Style文件";
            if (dlgOpenStyle.ShowDialog() == DialogResult.OK)
            {
                this.stylepath.Text = null;
                this.stylepath.Text = dlgOpenStyle.FileName;
            }
        }

        //设置坐标系
        private void selectscode_Click(object sender, EventArgs e)
        {
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);

            IGeographicCoordinateSystem pGeoRefSys;
            ISpatialReferenceDialog pDialog;
            pDialog = new SpatialReferenceDialogClass();

            pGeoRefSys = pDialog.DoModalCreate(true, false, false, 0) as IGeographicCoordinateSystem;

            if (pGeoRefSys != null)
            {
                coordinatetext.Text = pGeoRefSys.Name;
                GCScode = pGeoRefSys.FactoryCode;
            }
        }

        //转换工程文件
        private void convert_Click(object sender, EventArgs e)
        {
            string filepath = this.mpjpath.Text; //MPJ文件路径
            string savepath = this.savepath.Text;  //转换后的保存路径
            string style = this.stylepath.Text;   //style文件路径

            //开始转换
            if (filepath.Trim() == "" || savepath.Trim() == "" || style.Trim() == "")
            {
                MessageBox.Show("需要转换的工程文件、保存路径以及sytle路径不能为空！", "友情提示");
                return;
            }

            //加载工程文件
            this.axMxWorkSpace1.Open(filepath, WorkSpace.EnumOpenMode.OpenNormal);

            //取地图
            XMap map = this.axMxWorkSpace1.ActiveMap;
            //MessageBox.Show(map.ReferenceScale+"：比例尺");
            string mapname = map.Name;

            //各图层名称数组
            string[] names = new string[map.LayerCount];
            for (int i = 1; i < map.LayerCount + 1; i++)
            {
                names[i - 1] = map.get_Layer(i).LayerName;
            }

            //添加license
            ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.Desktop);
            IAoInitialize m_iao = new AoInitializeClass();
            m_iao.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);

            //生成MXD文件
            m_MapDocument = new MapDocumentClass();
            string mxdpath = savepath + "\\" + mapname + ".mxd";
            m_MapDocument.New(mxdpath);

            IMap imap = m_MapDocument.get_Map(0);
            imap.Name = mapname;  //设置arggis地图名字

            //设置比例尺
            if (plottext.Text != "")
            {
                imap.ReferenceScale = double.Parse(plottext.Text);
            }

            //加载style文件
            //IStyleGallery styleGallery = new StyleGalleryClass();
            //IStyleGalleryStorage styleGalleryStorage = styleGallery as IStyleGalleryStorage;
            //styleGalleryStorage.AddFile(style);
            //styleGalleryStorage.TargetFile = style;


            StyleSymbolMap styleSymbolMap = new StyleSymbolMap();
            styleSymbolMap.LoadStyle(style);

            //进度条
            this.progressBar1.Minimum = 1;
            this.progressBar1.Maximum = map.LayerCount + 1;

            //取地图的各个图层
            for (int i = 1; i < map.LayerCount + 1; i++)
            {
                this.progressBar1.Value = i + 1;

                IXMapLayer maplayer = map.get_Layer(i);
                string layername = names[i - 1];
                int len = layername.Length;
                string layertype = layername.Substring(len - 2, 2).ToLower();

                //将mpj图层转换为组图层
                IXGroupLayer groupLayer = (IXGroupLayer)maplayer;
                if (layertype.Equals("wp"))
                {
                    ////读区图层并转换
                    RegConvert reg = new RegConvert();
                    reg.SetStyleSymbolMap(styleSymbolMap);
                    ////reg.readWP(groupLayer, layername, imap, savepath, m_MapDocument, style, styleGallery, GCScode);
                    reg.readWP(groupLayer, layername, imap, savepath, m_MapDocument, style, GCScode);

                }
                else if (layertype.Equals("wl"))
                {
                    //读线图层并转换
                    LinConvert lin = new LinConvert();
                    lin.SetStyleSymbolMap(styleSymbolMap);
                    lin.readWL(groupLayer, layername, imap, savepath, m_MapDocument, style, GCScode);

                }
                else if (layertype.Equals("wt"))
                {
                    ////读点图层并转换
                    PntConvert pnt = new PntConvert();
                    pnt.SetStyleSymbolMap(styleSymbolMap);
                    pnt.readWT(groupLayer, layername, imap, savepath, m_MapDocument, style,GCScode);

                    ////读取注记信息
                    AnnConvert ann = new AnnConvert();
                    ann.readAnn(groupLayer, layername, imap, savepath,style, GCScode);
                }
            }

            //关闭style文件
            //styleGalleryStorage.RemoveFile(style);

            //保存地图
            m_MapDocument.Save();
            MessageBox.Show("转换完成！", "提示");
        }       
    }
}
